Imports Microsoft.VisualBasic
Imports System
Imports System.WEB
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.OleDb

Namespace MasterCms
    Public Class CTools

        ' HILFS-Funktionen      
        Public Function getIntag(ByVal str As String, ByVal tag As String, ByRef ixStart As Integer) As String
            'get content inside specified tag in given string
            '(searches first tag found in str from ixStart)
            str = getTag(str, tag, ixStart)
            'nun die Tags selber entfernen
            If str <> "-1" Then
                Dim tmpIx As Integer = ixStart
                tmpIx = str.IndexOf(">", 0) + 1
                Dim tagend As Integer = str.LastIndexOf("<")
                If tagend <> -1 Then
                    Return str.Substring(tmpIx, tagend - tmpIx)
                End If
            End If
            '(else)
            'ixStart = -1
            Return -1
        End Function

        Public Function getInTags(ByVal str As String, ByVal tag As String) As String()
            'return array of all complete tags (given tag) in string str
            '(will stop before end of string if finding malformed html, ie missing end tags)
            Dim count As Integer = -1
            Dim ixStart As Integer = 0
            Dim temp() As String
            ReDim temp(50)
            While ixStart <> -1
                count += 1
                If count > UBound(temp) Then ReDim Preserve temp(UBound(temp) + 50)
                temp(count) = getIntag(str, tag, ixStart)
            End While
            'remove last string in array "-1"
            ReDim Preserve temp(count - 1)
            Return temp
        End Function
        Public Function getTag(ByVal str As String, ByVal tag As String, ByRef ixStart As Integer) As String
            'get complete specified tag in given string (including the tag itself)
            ixStart = str.IndexOf("<" & tag, ixStart)
            If ixStart <> -1 Then
                'check if correct tag
                If str.Substring(ixStart + tag.Length + 1, 1) = " " Or str.Substring(ixStart + tag.Length + 1, 1) = ">" Then
                    'check if there is a tag end (somewhere)
                    Dim tagend As Integer = str.IndexOf("</" & tag & ">", ixStart)
                    If tagend <> -1 Then
                        tagend += tag.Length + 3
                        str = str.Substring(ixStart, tagend - ixStart)
                        ixStart = tagend
                        Return str
                    Else
                        'singular tag, just find first ">"
                        tagend = str.IndexOf(">", ixStart)
                        If tagend <> -1 Then
                            tagend += 1
                            str = str.Substring(ixStart, tagend - ixStart)
                            ixStart = tagend
                            Return str
                        End If
                    End If
                Else
                    'not correct tag, recurse to next
                    ixStart += tag.Length + 1
                    Return getTag(str, tag, ixStart)
                End If
            End If
            '(else)
            ixStart = -1
            Return -1
        End Function

        Public Shared Function ReadKommentar(ByVal pDatei As String, Optional ByVal bKonvert As Boolean = True) As String
            Dim fn As String = pDatei
            Dim objReader As StreamReader
            ReadKommentar = ""
            If File.Exists(fn) Then
                objReader = File.OpenText(fn)
                If bKonvert Then
                    ReadKommentar = objReader.ReadToEnd().Replace("<br/>", vbCrLf)
                Else
                    ReadKommentar = objReader.ReadToEnd()
                End If
                objReader.Close()
            End If
        End Function

        Public Shared Sub WriteKommentar(ByVal pDatei As String, ByVal pKommentar As String)
            Dim fn As String = pDatei
            Dim objWriter As StreamWriter
            objWriter = File.CreateText(fn)
            objWriter.Write(pKommentar.Replace(vbCrLf, "<br/>"))
            objWriter.Close()
        End Sub


        Public Shared Function GenerateRandomCode() As String
            Dim random As New Random()

            Dim captchaCode As String = ""
            For i As Integer = 0 To 4
                captchaCode = [String].Concat(captchaCode, random.[Next](10).ToString())
            Next
            Return captchaCode
        End Function
    End Class
End Namespace
